﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TimKiem
{
    public partial class frmTimKiemHoaDonBanBuon : Form
    {
        public frmTimKiemHoaDonBanBuon()
        {
            InitializeComponent();
        }
        public frmTimKiemHoaDonBanBuon(string maKho)
        {
            InitializeComponent();
            this.maKho = maKho;
        }
        public frmTimKiemHoaDonBanBuon(string maKho,string makhachhang)
        {
            InitializeComponent();
            this.maKho = maKho;
            this.makhachhang = makhachhang;
        }
        private string makhachhang = string.Empty;
        public string Makhachhang
        {
            get { return makhachhang; }
            set { makhachhang = value; }
        }
        private string maKho;
        public string MaKho
        {
            get { return maKho; }
            set { maKho = value; }
        }
        Entities.HDBanBuon[] bb = null;
        private List<Entities.HDBanBuon> banbuon;
        private void Select()
        {
            try
            {
                banbuon = new List<Entities.HDBanBuon>();
                bb = Common.StaticVariable.InterfaceObject.SelectHDBanBuon();
                if (bb == null)
                { return; }
                foreach (var item in bb)
                {
                    if (item.MaKho.ToUpper() == this.maKho.ToUpper())
                    {
                        banbuon.Add(item);
                    }
                }
                if (banbuon.Count == 0)
                {
                    this.Gridview();
                    return; 
                }
                dgvHienThi.DataSource = banbuon.ToArray();
                this.Gridview();

            }
            catch (Exception)
            {
            }
        }

        private void select_MaKhachHang()
        {
            try
            {
                banbuon = new List<Entities.HDBanBuon>();
                bb = Common.StaticVariable.InterfaceObject.SelectHDBanBuon();
                if (bb == null)
                { return; }
                foreach (var item in bb)
                {
                    if (item.MaKho.ToUpper() == this.maKho.ToUpper() && item.MaKhachHang.ToUpper() ==this.makhachhang.ToUpper())
                    {
                        banbuon.Add(item);
                    }
                }
                if (banbuon.Count == 0)
                {
                    this.Gridview();
                    return;
                }
                dgvHienThi.DataSource = banbuon.ToArray();
                this.Gridview();

            }
            catch (Exception)
            {
            }
        }
        private void Gridview()
        {
            try
            {
                if (banbuon.Count == 0)
                {
                    Entities.HDBanBuon[] nv = new Entities.HDBanBuon[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["MaHDBanBuon"].Visible = true;
                dgvHienThi.Columns["MaHDBanBuon"].HeaderText = "Mã hóa đơn";
                dgvHienThi.Columns["NgayLap"].Visible = true;
                dgvHienThi.Columns["NgayLap"].HeaderText = "Ngày bán";
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        private void frmTimKiemHoaDonBanBuon_Load(object sender, EventArgs e)
        {
            if (makhachhang != string.Empty)
            {
                this.select_MaKhachHang();
            }
            else
            {
                this.Select();
            }
        }
        string MaHDBanBuon = string.Empty;
        private void dgvHienThi_DoubleClick(object sender, EventArgs e)
        {
            this.Chon();
        }
        private void Chon()
        {
            try
            {
                if (this.banghi == null)
                {
                    MessageBox.Show("Bạn phải chọn hóa đơn.", "Thông báo");
                    return;
                }
                Common.StaticVariable.timhoadonbanbuon = banghi;
                this.Close();
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        private Entities.HDBanBuon banghi = null;
        private void GetRow()
        {
            try
            {
                if (banbuon == null) { return; }
                foreach (var item in banbuon.ToArray())
                {
                    if (item.MaHDBanBuon == this.MaHDBanBuon)
                    {
                        banghi = item;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvHienThi.Rows.Count <= 0)
            { return; }
            MaHDBanBuon = dgvHienThi.CurrentRow.Cells["MaHDBanBuon"].Value.ToString();
            this.GetRow();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            this.Chon();
        }

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        Common.StaticVariable.nhacungcap = null;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }
    }
}
